package com.tocean.app.controller.common;

import com.tocean.app.controller.request.paper.PaperOperateDto;
import com.tocean.app.controller.request.paper.PaperSearchDto;
import com.tocean.app.controller.request.paper.PaperTopicDto;
import com.tocean.app.dto.request.BasePageRequest;
import com.tocean.app.dto.response.BasePageResponse;
import com.tocean.app.dto.response.BaseResponse;

import com.tocean.app.entity.common.TestPaper;
import com.tocean.app.service.common.PaperService;
import com.tocean.app.service.common.TopicService;
import com.tocean.app.util.TopicFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "试卷管理")
@Slf4j
@RestController
@RequestMapping("/common/paper")
public class PaperController {

    @Autowired
    private PaperService paperService;
    @Autowired
    private TopicService topicService;


    @ApiOperation(value = "查询试卷")
    @PostMapping("/page")
    public BaseResponse<BasePageResponse<TestPaper>> page(@Valid @RequestBody BasePageRequest<PaperSearchDto> request) {
        return BaseResponse.success(paperService.page(request.getData(), request.getPage(), request.getLimit()));
    }

    @ApiOperation(value = "根据学科、年级、年级分类查询试卷")
    @PostMapping("/page/value")
    public BaseResponse<BasePageResponse<TestPaper>> pageByValue(@Valid @RequestBody BasePageRequest<PaperSearchDto> request) {
        return BaseResponse.success(paperService.pageByValue(request.getData(), request.getPage(), request.getLimit()));
    }

    @ApiOperation(value = "根据[试卷id]查询试卷")
    @GetMapping("/{paperId}")
    public BaseResponse<PaperOperateDto> findPaperById(@PathVariable("paperId") String paperId) {
        return BaseResponse.success(paperService.findPaperById(paperId));
    }


    @ApiOperation(value = "新增/更新试卷")
    @PostMapping
    public BaseResponse<?> saveOrUpdate( @RequestBody PaperOperateDto operateDto) {
        paperService.saveOrUpdate(operateDto);
        return BaseResponse.success();
    }

    @ApiOperation(value = "试卷新增题干")
    @PostMapping("/addTopic")
    public BaseResponse<PaperTopicDto> addTopic(@Valid @RequestBody PaperTopicDto paperTopicDto) {
        paperService.addStem(paperTopicDto);
        return BaseResponse.success();
    }

    @ApiOperation(value = "试卷新增题干")
    @PostMapping("/addTopic/{paperId}/{stemId}")
    public BaseResponse<PaperTopicDto> addOneTopic(@PathVariable("paperId") String paperId,@PathVariable("stemId") String stemId) {
        paperService.addOneStem(paperId,stemId);
        return BaseResponse.success();
    }

    @ApiOperation(value = "根据试卷id查询题目列表")
    @GetMapping("/{paperId}/topic")
    public BaseResponse<?> findPaperTopicById(@PathVariable("paperId") String paperId) {
        return BaseResponse.success(paperService.findPaperTopicById(paperId));
    }
    @ApiOperation(value = "根据试卷id,题目id查询是否存在记录")
    @GetMapping("/{paperId}/hastopic/{stemId}")
    public BaseResponse<?> isHasStem(@PathVariable("paperId") String paperId,@PathVariable("stemId") String stemId) {
        return BaseResponse.success(paperService.isHasStem(paperId,stemId));
    }
    @ApiOperation(value = "根据试卷id删除题目记录")
    @DeleteMapping("/{paperId}/rf")
    public BaseResponse<?> deleteTopic(@PathVariable("paperId") String paperId) {
        paperService.deleteTopic(paperId);
        return BaseResponse.success();
    }

//    @ApiOperation(value = "根据试卷id查询题目内容")
//    @GetMapping("/{paperId}/topic")
//    public BaseResponse<?> findTopicContentById(@PathVariable("paperId") String paperId) {
//        return BaseResponse.success(topicFeign.getData(paperId));
//    }
    @ApiOperation(value = "根据题目id查询题目内容")
    @GetMapping("/topic/{stemId}")
    public BaseResponse<?> findTopicContentById(@PathVariable("stemId") Long stemId) {
        topicService.showData(stemId);
        return BaseResponse.success();
    }


    @ApiOperation(value = "根据ID删除试卷及其下题目")
    @DeleteMapping("/rf/{paperId}")
    public BaseResponse<?> delete(@PathVariable("paperId") String paperId) {
        paperService.delete(paperId);
        return BaseResponse.success();
    }

}
